package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.PageResponseResult;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.mappers.CourseBaseMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.web.interceptor.AuthInfoHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 课程基本信息 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-16
 */
@Service
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseService {
    //分页查询当前机构的课程列表
    @Override
    public ResponseResult courseList(PageRequest request, CourseDto dto) {
        //创建分页参数
        Page<CourseBase>pageParam=new Page<>(request.getPageNo(),request.getPageSize());
        //创建查询条件
        LambdaQueryWrapper<CourseBase>qw=new LambdaQueryWrapper<>();
        String courseName = dto.getCourseName();
        if (!StringUtils.isEmpty(courseName)) {
            qw.like(CourseBase::getName,"%"+courseName+"%");
        }
        String auditStatus = dto.getAuditStatus();
        if (!StringUtils.isEmpty(auditStatus)) {
            qw.eq(CourseBase::getAuditStatus,auditStatus);
        }
        String teachmode = dto.getTeachmode();
        if (!StringUtils.isEmpty(teachmode)) {
            qw.eq(CourseBase::getTeachmode,teachmode);
        }
        //设置机构id
        qw.eq(CourseBase::getCompanyId, AuthInfoHolder.getCompanyId());
        //分页查询
        IPage<CourseBase>iPage=this.page(pageParam,qw);//分页起始条数page，查询条件QueryWrapper
        //转化PageResponseResult
        return PageResponseResult.okResult(iPage.getTotal(),iPage.getRecords());
    }
}
